/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */

package com.hopes.bizz.poll.factor.controller;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hopes.bizz.poll.factor.entity.PollDisplayFactorEntity;
import com.hopes.bizz.poll.factor.service.PollDisplayFactorService;
import com.hopes.boss.common.core.util.R;
import com.hopes.boss.common.excel.annotation.ResponseExcel;
import com.hopes.boss.common.log.annotation.SysLog;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 数据展示因子
 *
 * @author ljh
 * @date 2023-05-22 10:00:56
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/displayFactor")
@Tag(description = "展示因子", name = "数据展示因子管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class PollDisplayFactorController {

    private final PollDisplayFactorService pollDisplayFactorService;

    /**
     * 分页查询
     *
     * @param page              分页对象
     * @param pollDisplayFactor 数据展示因子
     * @return
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('poll_displayFactor_view')")
    public R getpollDisplayFactorPage(Page page, PollDisplayFactorEntity pollDisplayFactor) {
        LambdaQueryWrapper<PollDisplayFactorEntity> wrapper = Wrappers.lambdaQuery();
        if (StringUtils.isNotBlank(pollDisplayFactor.getMonitorType())) {
            wrapper.eq(PollDisplayFactorEntity::getMonitorType, pollDisplayFactor.getMonitorType());
        }
        return R.ok(pollDisplayFactorService.page(page, wrapper));
    }


    /**
     * 通过id查询数据展示因子
     *
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('poll_displayFactor_view')")
    public R getById(@PathVariable("id") Long id) {
        return R.ok(pollDisplayFactorService.getById(id));
    }


    /**
     * 通过monitorType查询展示因子
     *
     * @param  monitorType
     * @return R
     */
    @Operation(summary = "通过monitorType查询", description = "通过monitorType查询")
    @GetMapping("/getByMonitorType/{monitorType}")
    @PreAuthorize("@pms.hasPermission('poll_displayFactor_view')")
    public R getByMonitorType(@PathVariable("monitorType") String monitorType) {
        return R.ok(pollDisplayFactorService.getByMonitorType(monitorType));
    }

    @Operation(summary = "通过监控点大类查询", description = "通过监控点大类查询")
    @GetMapping("/getByMonitorTypeMain/{monitorTypeMain}")
    @PreAuthorize("@pms.hasPermission('poll_displayFactor_view')")
    public R getByMonitorTypeMain(@PathVariable("monitorTypeMain") Integer monitorTypeMain) {
        return R.ok(pollDisplayFactorService.getByMonitorTypeMain(monitorTypeMain));
    }

    /**
     * 新增数据展示因子
     *
     * @param pollDisplayFactor 数据展示因子
     * @return R
     */
    @Operation(summary = "新增数据展示因子", description = "新增数据展示因子")
    @SysLog("新增数据展示因子")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('poll_displayFactor_add')")
    public R save(@RequestBody PollDisplayFactorEntity pollDisplayFactor) {
        return R.ok(pollDisplayFactorService.save(pollDisplayFactor));
    }

    /**
     * 修改数据展示因子
     *
     * @param pollDisplayFactor 数据展示因子
     * @return R
     */
    @Operation(summary = "修改数据展示因子", description = "修改数据展示因子")
    @SysLog("修改数据展示因子")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('poll_displayFactor_edit')")
    public R updateById(@RequestBody PollDisplayFactorEntity pollDisplayFactor) {
        return R.ok(pollDisplayFactorService.updateById(pollDisplayFactor));
    }

    /**
     * 通过id删除数据展示因子
     *
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除数据展示因子", description = "通过id删除数据展示因子")
    @SysLog("通过id删除数据展示因子")
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('poll_displayFactor_del')")
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(pollDisplayFactorService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     *
     * @param pollDisplayFactor 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('poll_displayFactor_export')")
    public List<PollDisplayFactorEntity> export(PollDisplayFactorEntity pollDisplayFactor) {
        return pollDisplayFactorService.list(Wrappers.query(pollDisplayFactor));
    }
}
